// pages/index-new/index.js
const rr = require("../../utils.js");
let navigationBar;
let catGoods;
let goodsList;
let head;
Page({
  data: {
    shop_id: -1,
    block_list: [],
    activity_list: [],
    goods_num:1,
    activity_status: 0,
    loadingStatus: 0,
    page: 1,
    goods_list: null,
    is_diy: -1,
    cat_list: null,
    is_load: 0,
    bg_color: '#f6f6f6'
  },

  onLoad: function (t) {
    
    getApp().pageOnLoad(this, t);
    var that = this;
    wx.getSystemInfo({
      success (res) {
        wx.setStorageSync('headheight', res.statusBarHeight)
        that.setData({
            headheight: res.statusBarHeight
        })
      }
    })
    var shop = wx.getStorageSync('address_info');
    var shop_id = shop && shop.shop_id ? shop.shop_id : 0
    this.setData({
      shop_id: shop_id
    })
    
    if (t.scene) {
      var m = decodeURIComponent(t.scene);
      var hh = rr.scene_decode(m);
      wx.setStorageSync("scene", hh)
      this.updateShop(hh);
    } else {
      this.getDiyMode(t);
    }
  },
  // 切换社区
  updateShop(hh) {
    var thst = this;
    if (hh.shop_id != '' && hh.shop_id != undefined && hh.shop_id != null && hh.shop_id != 0) {
      var shop = wx.getStorageSync('address_info');
      var shop_id = shop && shop.shop_id ? shop.shop_id : 0
      if (shop_id == 0 && hh.shop_id > 0) {
        this.updateShopSubimt(hh.shop_id);
      }else if(shop_id > 0 && hh.shop_id > 0 && shop_id != hh.shop_id && thst.data.store.update_shop == 1 ) {
        getApp().request({
          url: getApp().api.default.index_position,
          data: {
            tz_id: hh.shop_id,
          },
          method: "POST",
          success: function (t) {
            wx.showModal({
              title: '提示',
              content: '是否切换到' + t.name + '社区',
              success: function (res) {
                if (res.confirm) {
                  wx.removeStorage({
                    key: 'address_info'
                  })
                  thst.updateShopSubimt(hh.shop_id)
                }
                thst.getDiyMode(t);
              }
            })
            return false;
          },
          
        })
      }else {
        thst.getDiyMode();
      }
    } else {
      thst.getDiyMode();
    }
  },
  // 确认切换社区
  updateShopSubimt: function (shop_id) {
    var that = this;
    getApp().request({
        url: getApp().api.default.index_position,
        data: {
          tz_id: shop_id,
          is_update: 1,
        },
        method: "POST",
        success: function (t) {
          wx.setStorage({
            key: "address_info",
            data: {
              nickname: t.nickname,
              name: t.name,
              address: t.address,
              logo: t.pic_url,
              shop_id: t.id,
              peisong_type: t.peisong_type,
              mobile: t.mobile
            }
          })
          
          return false;
        },
        complete: function (res) {
          wx.reLaunch({
            url: "/pages/index/index",
          })
        }
    })
  },
  // 判断是否已经添加diy首页
  getDiyMode: function () {
    wx.showLoading({
      title: "加载中"
    })
    var shop = wx.getStorageSync('address_info');
    if (!(shop && shop.shop_id)) {
      this.setData({
        is_load: 1
      })
    }
    var that = this;
    getApp().request({
      url: getApp().api.index.is_diy_index,
      data: {
        shop_id: that.data.shop_id,
      },
      success: function(t) {
        if (t.code == 0) {
          that.setData({
            is_diy: 0
          })
          that.getIndexMode()
        }else {
          that.setData({
            is_diy: 1
          })
          that.getDiyData()
        }
      },
      complete: function() {
        setTimeout(function() {
          navigationBar = that.selectComponent("#navigationBar")
          catGoods = that.selectComponent("#catGoods")
          goodsList = that.selectComponent("#goodsList")
          head = that.selectComponent("#head")
        },1000)
      }
    })
  },
  // 获取首页模块
  getIndexMode: function () {
    var that = this;
    // wx.showLoading({
    //   title: "加载中"
    // })
    var shop = wx.getStorageSync('address_info');
    this.setData({
      shop_id: shop && shop.shop_id ? shop.shop_id : 0
    })
    getApp().request({
      url: getApp().api.index.diy_mode,
      data: {
        shop_id: that.data.shop_id,
      },
      success: function(t) {
        that.setData({
          module_list: t.data.diy_mode,
          update_list: t.data.update_list,
          index_alert: t.data.index_alert,
          store: t.data.store,
        })
        t.data.diy_mode.forEach(item => {
          // if (item.name == 'cat') {
          //   that.getDiyInfoCat()
          // }
        });
        // 获取数据
        // that.getDiyInfo(t.data.diy_mode)
      },
      complete: function() {
        wx.hideLoading();
      }
    })
  },
  getDiyData: function () {
    var that = this;
    // wx.showLoading({
    //   title: "加载中"
    // })
    var shop = wx.getStorageSync('address_info');
    this.setData({
      shop_id: shop && shop.shop_id ? shop.shop_id : 0
    })
    getApp().request({
      url: getApp().api.diy.diy_info,
      data: {
        shop_id: that.data.shop_id,
        page_id: -1
      },
      success: function(t) {
        if (t.code == 0) {
          that.setData({
            template: t.data.template,
            index_alert: t.data.index_alert,
            bg_color: t.data.bg_color
          })
        }
      },
      complete: function() {
        wx.hideLoading();
      }
    })
  },
  getDiyInfoCat: function () {
    var that = this;
    var shop = wx.getStorageSync('address_info');
    var shop_id = shop && shop.shop_id ? shop.shop_id : 0
    var item = {
      name: 'cat',
      shop_id: shop_id
    };
    getApp().request({
      url: getApp().api.index.get_diy_info,
      data: item,
      success: function(t) {
        that.data.cat_id = 0;
        if ( t.data.list.activity_goods != null 
          && t.data.list.activity_goods != [] 
          && t.data.list.activity_goods != '' 
          && t.data.list.activity_goods != undefined
          ) {
          that.setData({
            activity_status: "now_goods",
            goods_list: t.data.list.activity_goods.now_goods.goods_list,
          })
        } else {
          that.setData({
            tabs: t.data.list.cat_list,
            goods_list: t.data.list.cat_list[0].goods_list,
          })
        }
      },
      complete: function() {
        
      }
    })
  },

  
  
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().pageOnShow(this)
    var bayUser = this.selectComponent("#bayUser")
    bayUser.data.getInfo = true;
    var that = this;
    if (that.data.store) {
      wx.setNavigationBarTitle({
        title: that.data.store.name
      })
    }
    var shop = wx.getStorageSync('address_info');
    if ((shop && shop.shop_id && this.data.is_load) || (shop && shop.shop_id != this.data.shop_id)) {
      wx.reLaunch({
        url: "/pages/index/index",
      })
    }
    this.show_navs();
    this.cart_num();
  },
  show_navs: function() {
    var that = this;
    var nav = wx.getStorageSync("_navbar");
    if (!nav) {
      setTimeout(function() {
        that.show_navs();
      },1000);
    }
    var url = "/pages/index/index";
    if (nav == '' || nav == undefined) return false;
    for (var i = 0; i < nav.navs.length; i++) {
      if (nav.navs[i].url == url) {
        if (getApp().globalData.nav_index == i) {
          if (typeof that.getTabBar === 'function' &&
              that.getTabBar()) {
            that.getTabBar().setData({
              selected: getApp().globalData.nav_index,
              is_show:1
            })
          }
        } else {
          getApp().globalData.nav_index = i;
          if (typeof that.getTabBar === 'function' &&
              that.getTabBar()) {
            that.getTabBar().setData({
              selected: getApp().globalData.nav_index,
              is_show: 1
            })
          }
        }
      }
    }
  },
  // 获取购物车数量
  cart_num: function () {
    var that = this;
    wx.getStorage({
      key: 'user_info',
      success: function(res) {
        getApp().request({
          url: getApp().api.cart.list,
          success: function (t) {
            if (0 == t.code) {
              if (typeof that.getTabBar === 'function' &&
                  that.getTabBar()) {
                if (t.data.mch_list.length > 0) {
                  var length = t.data.list.length + t.data.mch_list[0].list.length;
                } else {
                  var length = t.data.list.length
                }
                that.getTabBar().setData({
                  cart_num: length
                })
              }
            }
          }
        });
      },
    })
  },

  onHide: function () {
    var bayUser = this.selectComponent("#bayUser")
    bayUser.data.getInfo = false;
  },

  onUnload: function () {
    var bayUser = this.selectComponent("#bayUser")
    bayUser.data.getInfo = false;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.is_diy == 0 && goodsList) {
      goodsList.lower()
    }else if(this.data.is_diy == 0 && !goodsList) {
      goodsList = this.selectComponent("#goodsList")
      goodsList && goodsList.lower()
    }
    if (this.data.is_diy == 1 && catGoods) {
      catGoods.getGoodsList()
    }else if(this.data.is_diy == 1 && !catGoods) {
      catGoods = this.selectComponent("#catGoods"),catGoods && void 0 !== catGoods.lower && catGoods.lower()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var i = this;
    getApp().request({
      url: getApp().api.default.login_style,
      method: "POST",
      success: function (t) {
        if (t != 0) {
          1 == ++e && getApp().shareSendCoupon(i);
        }
      },
    })
    var shop = wx.getStorageSync('address_info');
    var shop_id = i.data.shop_id ? i.data.shop_id : 0;
    var user_id = wx.getStorageSync("user_info").id ? wx.getStorageSync("user_info").id : 0;
    return {
      // path: "/pages/index/index?user_id=" + wx.getStorageSync("user_info").id,
      path: "/pages/index/index?scene=shop_id:" + shop_id + ",user_id:"+  user_id,
      imageUrl: void 0 === i.data.store.share_bg ? '' : i.data.store.share_bg,
      title: shop && shop.shop_id ? shop.name : i.data.store.name ,
      success: function (t) {
        
      },
    };
  },
  onShareTimeline: function () {
    var i = this;
    var shop_id = i.data.shop_id ? i.data.shop_id : 0;
    var user_id = wx.getStorageSync("user_info").id ? wx.getStorageSync("user_info").id : 0;
    return {
      query: {scene:'shop_id:' + shop_id,'user_id':user_id},
      imageUrl: void 0 === i.data.store.share_bg ? '' : i.data.store.share_bg,
      title: i.data.store.name ,
    };
  },
  // 页面滚动
  onPageScroll: function (e) {
    var that = this;
    if (!navigationBar && !head) {
      navigationBar = that.selectComponent("#navigationBar")
      catGoods = that.selectComponent("#catGoods")
      goodsList = that.selectComponent("#goodsList")
      head = that.selectComponent("#head")
    }else {
      if (this.data.is_diy == 1) {
        var scrollTop = e.scrollTop
        if (navigationBar) {
          navigationBar.setOpacity(scrollTop, 200)
        }else if(head) {
          head.setOpacity(scrollTop);
        }
      }
    }
  },
  getIndexQrcode: function() {
    var a = this;
    if (a.setData({
        goods_qrcode_active: "active",
        share_modal_active: ""
      }), a.data.goods_qrcode) return !0;
    getApp().request({
      url: getApp().api.default.index_qrcode,
      data: {
        type: 5
      },
      success: function(t) {
        0 == t.code && a.setData({
          goods_qrcode: t.data.pic_url
        }), 1 == t.code && (a.goodsQrcodeClose(), wx.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          success: function(t) {
            t.confirm;
          }
        }));
      }
    });
  },
  goodsQrcodeClose: function() {
    this.setData({
      goods_qrcode_active: "",
      no_scroll: !1
    });
  },
  // 显示分享
  showShareModal: function() {
    this.setData({
      share_modal_active: "active",
      no_scroll: !0
    });
  },
  shareModalClose: function() {
    this.setData({
      share_modal_active: "",
      no_scroll: !1
    });
  },
})